//
//  KZExamItemView.m
//  lg_zhinengbeikao_english
//
//  Created by 朱凯 on 2018/12/27.
//  Copyright © 2018 zhukai. All rights reserved.
//

#import "KZExamItemView_v2.h"

#import "KZExamItemSubjectView.h"       // 题干
#import "KZExamItemAnswerView.h"        // 作答视图



@interface KZExamItemView_v2()


/// 题干区滑动视图
@property (nonatomic, strong) KZExamItemSubjectView *subjectView;
/// 答题区
@property (nonatomic, strong) KZExamItemAnswerView *answerView;

/// 滑块视图
@property (nonatomic, strong) UIView *panBlockView;
/// 刚开始拖动时的滑块底部位置
@property (nonatomic, assign) CGFloat theBottomWhilePanBegan;
@end

@implementation KZExamItemView_v2
{
    CGFloat _panBlockViewMaxTopDistance;        // 滑块底部距离视图顶部最大距离
    CGFloat _panBlockViewMaxBottomDistance;     // 滑块底部距离视图底部最大距离
}

#pragma mark - 初始化
- (instancetype)init {
    
    if (self = [super init]) {
        _panBlockViewMaxTopDistance = 50;
        _panBlockViewMaxBottomDistance = 0;
        [self setupUI];
    }
    return self;
}

#pragma mark - 创建视图
- (void)setupUI {
    
    // 滑块，用来控制题干区域和作答区域的范围
    _panBlockView = [UIView new];
    _panBlockView.backgroundColor = UIColor.blackColor;
    [self addSubview:_panBlockView];
    [_panBlockView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(30);
        make.width.mas_equalTo(80);
        make.bottom.equalTo(self.mas_centerY);
        make.centerX.equalTo(self);
    }];
    UIPanGestureRecognizer *pan = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(pan:)];
    [_panBlockView addGestureRecognizer:pan];

    UIView *line = [UIView new];
    line.backgroundColor = kLineColor;
    [_panBlockView addSubview:line];
    [line mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.right.equalTo(self);
        make.bottom.equalTo(self.panBlockView);
        make.height.equalTo(@1);
    }];
    
    _subjectView = [KZExamItemSubjectView new];;
    [self addSubview:_subjectView];
    [_subjectView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.left.mas_equalTo(0);
        make.width.equalTo(self);
        make.bottom.equalTo(self.panBlockView);
    }];
    
    _answerView = [KZExamItemAnswerView new];
    [self addSubview:_answerView];
    [_answerView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.width.bottom.equalTo(self);
        make.top.equalTo(self.panBlockView.mas_bottom);
    }];
    
    [self bringSubviewToFront:_panBlockView];
}



#pragma mark swipe gesture
- (void)pan:(UIPanGestureRecognizer *)sender {
    
    // 拖动偏移量
    CGFloat panOffsetY = 0;
    switch (sender.state) {
        case UIGestureRecognizerStateBegan:
            // 拖动开始时记录滑块底部的位置
            _theBottomWhilePanBegan = _panBlockView.bottom;
            // 初始化偏移量
            panOffsetY = 0;
            break;
        case UIGestureRecognizerStateChanged:
        {
            // 修改拖动偏移量，且偏移量需要满足偏移范围
            panOffsetY = [sender translationInView:self].y;
            CGFloat offsetY = _theBottomWhilePanBegan + panOffsetY;
            offsetY = MIN(self.height - _panBlockViewMaxBottomDistance, MAX(_panBlockViewMaxTopDistance, offsetY));
            
            // 更新滑块的约束，题干和作答区跟随变动
            [_panBlockView mas_updateConstraints:^(MASConstraintMaker *make) {
                make.bottom.equalTo(self.mas_top).offset(offsetY);
            }];
        }
            break;
        default:
            break;
    }
    
}

#pragma mark - setter
// 设置数据model
- (void)setModel:(KZExamItemModel *)model {
    
    _model = model;
    _subjectView.model = model;
    _answerView.model = model;
    
    // 作文题的底部
    if (_model.type == KZExamItemWritting) {
        _panBlockViewMaxBottomDistance = H(250);
    }
}



@end


